{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "NiN块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import d2lzh as d2l\n",
    "from mxnet import gluon, init, nd\n",
    "from mxnet.gluon import nn\n",
    "\n",
    "def nin_block(num_channels, kernel_size, strides, padding):\n",
    "    blk = nn.Sequential()\n",
    "    blk.add(nn.Conv2D(num_channels, kernel_size,\n",
    "                     strides, padding, activation='relu'),\n",
    "           nn.Conv2D(num_channels, kernel_size=1, activation='relu'),\n",
    "           nn.Conv2D(num_channels, kernel_size=1, activation='relu'))\n",
    "    return blk"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "NiN模型"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "net = nn.Sequential()\n",
    "net.add(nin_block(96, kernel_size=11, strides=4, padding=0),\n",
    "       nn.MaxPool2D(pool_size=3, strides=2),\n",
    "       nin_block(256, kernel_size=5, strides=1, padding=2),\n",
    "       nn.MaxPool2D(pool_size=3, strides=2),\n",
    "       nin_block(384, kernel_size=3, strides=1, padding=1),\n",
    "       nn.MaxPool2D(pool_size=3, strides=2),\n",
    "       nn.Dropout(0.5),\n",
    "        #标签类别是10\n",
    "       nin_block(10, kernel_size=3, strides=1, padding=1),\n",
    "        #全局平均池化层将窗口形状自动设置成为输入的高和宽\n",
    "       nn.GlobalAvgPool2D(),\n",
    "        #将思维的输出转成二维的输出，其形状为（批量大小，10）\n",
    "       nn.Flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequential1 output shape:\t (1, 96, 54, 54)\n",
      "pool0 output shape:\t (1, 96, 26, 26)\n",
      "sequential2 output shape:\t (1, 256, 26, 26)\n",
      "pool1 output shape:\t (1, 256, 12, 12)\n",
      "sequential3 output shape:\t (1, 384, 12, 12)\n",
      "pool2 output shape:\t (1, 384, 5, 5)\n",
      "dropout0 output shape:\t (1, 384, 5, 5)\n",
      "sequential4 output shape:\t (1, 10, 5, 5)\n",
      "pool3 output shape:\t (1, 10, 1, 1)\n",
      "flatten0 output shape:\t (1, 10)\n"
     ]
    }
   ],
   "source": [
    "#构建一个数据样本查看每一层的输出形状\n",
    "X = nd.random.uniform(shape=(1, 1, 224, 224))\n",
    "net.initialize()\n",
    "for layer in net:\n",
    "    X = layer(X)\n",
    "    print(layer.name, 'output shape:\\t', X.shape)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training on cpu(0)\n"
     ]
    }
   ],
   "source": [
    "lr, num_epochs, batch_size, ctx = 0.1, 5, 128, d2l.try_gpu()\n",
    "net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier())\n",
    "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr})\n",
    "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)\n",
    "d2l.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
